<template>
  <a-modal
    title="编辑人员"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <div style="display: flex; flex-direction: row; justify-content: space-around; align-items: center">
          <div style="width: 70%">
            <a-row :gutter="12">
              <a-col :md="12" :sm="24">
                <a-form-item
                  label="人员姓名"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                >
                  <a-input v-decorator="['staffName', {initialValue: staff.staffName, rules: [{required: true, message: '请输入人员姓名'}]}]" />
                </a-form-item>
              </a-col>
              <a-col :md="12" :sm="24">
                <a-form-item
                  label="性别"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                  :required="true"
                >
                  <a-radio-group v-decorator="['staffSex', {initialValue: staff.staffSex, rules: [{required: true, message: '请输入性别', trigger: 'blur'}]}]" >
                    <a-radio :value="'男'">男</a-radio>
                    <a-radio :value="'女'">女</a-radio>
                  </a-radio-group>
                </a-form-item>
              </a-col>
            </a-row>
            <a-row :gutter="12">
              <a-col :md="12" :sm="24">
                <a-form-item
                  label="人员部门"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                >
                  <a-tree-select
                    v-decorator="['deptId', {initialValue: staff.deptId, rules: [{required: true, message: '请选择部别'}]}]"
                    style="width: 100%"
                    :tree-data="deptsTree"
                    :dropdown-style="{ maxHeight: '300px', overflow: 'auto' }"
                    placeholder="请选择"
                    allow-clear
                    :treeDefaultExpandedKeys="expandedKeys"
                  />
                </a-form-item>
              </a-col>
              <a-col :md="12" :sm="24">
                <a-form-item
                  label="电话号码"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                >
                  <a-input v-decorator="['staffTel', {initialValue: staff.staffTel, rules: [{required: true, message: '请输入电话号码'}]}]" />
                </a-form-item>
              </a-col>
            </a-row>
            <a-row :gutter="12">
              <a-col :md="12" :sm="24">
                <a-form-item
                  label="出生日期"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                >
                  <a-date-picker
                    :format="'YYYY-MM-DD'"
                    :fullscreen="false"
                    @panelChange="onPanelChange"
                    v-decorator="['staffBirth', {initialValue: staff.staffBirth, rules: [{required: true, message: '请选择'}]}]"
                    style="width: 100%"/>
                </a-form-item>
              </a-col>
              <a-col :md="12" :sm="24">
                <a-form-item
                  label="备注信息"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                >
                  <a-input v-decorator="['staffInfo', {initialValue: staff.staffInfo, rules: [{message: '请输入备注信息'}]}]" />
                </a-form-item>
              </a-col>
            </a-row>
          </div>
          <div style="width: 30%; display: flex; justify-content: center; align-items: center" @click="handleImage()">
            <a-card :hoverable="true" style="width: 120px; height: 160px">
              <img
                height="160"
                slot="cover"
                alt="图像"
                :src="staff.staffImage"
              />
            </a-card>
          </div>
        </div>
      </a-form>
    </a-spin>
    <change-image ref="changeImage" @ok="handleOk"/>
  </a-modal>
</template>

<script>
import { editStaff } from '@/api/basic/staff'
import { TreeSelect } from 'ant-design-vue'
import ChangeImage from './ChangeImage'
import moment from 'moment'
export default {
  components: {
    ChangeImage,
    ATreeSelect: TreeSelect
  },
  props: {
    deptsTree: {
      type: Array,
      default () {
        return {}
      }
    }
  },
  data () {
    return {
      expandedKeys: [1],
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      sex: [
        {
          staffSex: '男'
        },
        {
          staffSex: '女'
        }],
      visible: false,
      confirmLoading: false,
      staff: {},
      form: this.$form.createForm(this)
    }
  },
  methods: {
    edit (record) {
      this.visible = true
      this.staff = record
      this.staff.staffBirth = moment(record.staffBirth, 'YYYY-MM-DD')
    },
    handleImage () {
      this.$refs.changeImage.upload()
    },
    onPanelChange (value, mode) {
      console.log(value, mode)
    },
    handleSubmit () {
      const _this = this
      const { form: { validateFields } } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          values.staffImage = _this.staff.staffImage
          editStaff(Object.assign(values, { 'staffId': this.staff.staffId }))
            .then(res => {
              if (res.data.message !== 'error') {
                this.visible = false
                this.confirmLoading = false
                this.form.resetFields()
                this.$emit('ok')
              } else {
                this.confirmLoading = false
                this.$message.error('操作失败！')
              }
            })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel () {
      this.form.resetFields()
      this.visible = false
    },
    handleOk () {
      // this.$refs.table.clearSelected()
      // this.$refs.experience.refresh(false)
      this.visible = false
    }
  }
}
</script>
